package dyy.zc.note.utils;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.util.Log;

import dyy.zc.note.bean.NoteInfo;

public class DBControl {

	DBHelper dbHelper;
	Context context;
	
	public DBControl(Context context) {
		this.context = context;
		dbHelper = new DBHelper(context);
	}
	
	
	public void insert(String content,String title,int type,String timeStr){
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("title", title);
		values.put("type", type);
		values.put("content", content);
		values.put("timeStr", timeStr);
		database.insert("note", null, values );
		database.close();
	}

	public void update(String id,String content,String title,int type,String timeStr){
		SQLiteDatabase database = dbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("title", title);
		values.put("type", type);
		values.put("content", content);
		values.put("timeStr", timeStr);
		String whereClause="_id=?";
		String [] whereArgs = {id};
		database.update("note", values, whereClause, whereArgs);
		database.close();
	}

	public void delete(String id){
		//得到一个可写的数据库
		SQLiteDatabase db =dbHelper.getReadableDatabase();
		String whereClauses = "_id=?";
		String [] whereArgs = {id};
//调用delete方法，删除数据
		db.delete("note", whereClauses, whereArgs);
		db.close();
	}

	public void query(List<NoteInfo> list) {
		list.clear();
		SQLiteDatabase database = dbHelper.getWritableDatabase();
//查询获得游标
		Cursor cursor = database.query ("note",null,null,null,null,null,null);
//参数1：表名
//参数2：要想显示的列
//参数3：where子句
//参数4：where子句对应的条件值
//参数5：分组方式
//参数6：having条件
//参数7：排序方式
//Cursor cursor = db.query("stu_table",
// new String[]{"id","sname","sage","ssex"},
// "id=?", new String[]{"1"}, null, null, null);
		//判断游标是否为空
			//遍历游标
			while(cursor.moveToNext()){
				NoteInfo noteInfo = new NoteInfo();
				String id = cursor.getString(cursor.getColumnIndex("_id"));
				String title = cursor.getString(cursor.getColumnIndex("title"));
				String content = cursor.getString(cursor.getColumnIndex("content"));
				String type = cursor.getString(cursor.getColumnIndex("type"));
//				String img = cursor.getString(cursor.getColumnIndex("img"));
				String timeStr = cursor.getString(cursor.getColumnIndex("timeStr"));
				noteInfo.setId(id);
				noteInfo.setTitle(title);
				noteInfo.setContent(content);
				noteInfo.setType(type);
				noteInfo.setTimeStr(timeStr);
				list.add(noteInfo);
				Log.e("cone",content);
			}
		database.close();
	}
}
